/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package it.kirys.rilego.engine.outputbuilders;

import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import it.kirys.rilego.engine.processors.ICompressor;
import it.kirys.rilego.engine.processors.PNGCompressor;
import it.kirys.rilego.engine.processors.ProcessorOutput;
import it.kirys.rilego.engine.project.Project;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Federico Improta
 */
public class PdfBuilder implements IOutputBuilder {

	private BufferedOutputStream out;
	private Document document;
	private IMediaType targetMedia;



	public PdfBuilder(IMediaType targetMedia) {
		this.targetMedia=targetMedia;
	}


	@Override
	public void open(OutputStream stream, Project project) throws BuilderException {
		this.out = new BufferedOutputStream(stream);
		MediaBoxSize mb = targetMedia.getMediaBoxSize();
		com.lowagie.text.Rectangle pageSize = new com.lowagie.text.Rectangle(mb.getWidthPoints(), mb.getHeightPoints());
		pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xFF));
		document = new Document(pageSize, 0, 0, 0, 0);
		document.setHeader(null);
		document.setFooter(null);
		try {
			PdfWriter p = PdfWriter.getInstance(document, out);
		} catch (DocumentException ex) {
			throw new BuilderException("Unable to initialize writer",ex);
		}
		//p.setFullCompression();
		if (project!=null){
		document.addTitle(project.getTitle()); // todo add title
		document.addAuthor(project.getAuthor()); //todo add author
		}
		//document.addCreator("JE-Comics "+EngineShortVersion+" output for "+ot.getTypeName());
		document.open();
		if (!document.setMargins(0, 0, 0, 0)) {
			throw new BuilderException("Unable to set document margins");
		}
	}

	@Override
	public void close() {
		document.close();
	}
   public void addPage() throws DocumentException{
		document.newPage();
	}

	public void testPage() throws DocumentException{
		addPage();
		com.lowagie.text.Chapter chp = new Chapter("TEST", 1);
		document.add(chp);
	}
	@Override
	public void add2Output(ProcessorOutput[] items) throws BuilderException {
		for (ProcessorOutput item : items) {
			com.lowagie.text.Image I;
			try {
				addPage();
				I = com.lowagie.text.Image.getInstance(item.toByteArray());
				//I.setAbsolutePosition(0, 0);
				MediaBoxSize mb = targetMedia.getMediaBoxSize();
				I.scaleToFit(mb.getWidthPoints(), mb.getHeightPoints());
				I.setAlignment(com.lowagie.text.Image.ALIGN_MIDDLE | com.lowagie.text.Image.ALIGN_CENTER);
				document.add(I);
			} catch (DocumentException ex) {
				throw new BuilderException("Unable to add image to document",ex);
			} catch (MalformedURLException ex) {
				throw new BuilderException("Unable to add image to document",ex);
			} catch (IOException ex) {
				throw new BuilderException("Unable to add image to document",ex);
			}
		}
	}

	@Override
	public OutputStream getOutputStream() {
		return out;
	}

	@Override
	public IMediaType getTargetMedia() {
		return targetMedia;
	}

	@Override
	public ICompressor getCompressor() {
		return new PNGCompressor();
	}

}
